package com.kgc.springboot.aviation.controller.jianghongkun;

import com.github.pagehelper.PageInfo;
import com.kgc.springboot.aviation.pojo.Consulting;
import com.kgc.springboot.aviation.pojo.Laws;
import com.kgc.springboot.aviation.pojo.Users;
import com.kgc.springboot.aviation.service.jianghongkun.LawsService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;

/**
 * ClassName: LawController <br/>
 * Description: <br/>
 * date: 2020/12/29 15:59<br/>
 *
 * @author 12161<br />
 * @since JDK 1.8
 */
@Controller
public class LawController {
    //@Resource依赖注入，从容器中自动获取bean
    @Resource
    LawsService lawsService;



    /**
    * Description: 后台法规列表  分页查询 <br/>
    * @author: 姜洪坤<br/>
    * @date: 2020/12/29 16:06<br/>
    * @param:<br/>
    * @return:
    */

    @RequestMapping("/findlaws")
    public String findLaws(@RequestParam(value = "lawNum",required = false,defaultValue = "") String lawNum,
                           @RequestParam(value = "lawName",required = false,defaultValue = "") String lawName,
                           @RequestParam(value = "pageNum",required = false,defaultValue = "1") Integer pageNum,
                           Model model,HttpServletRequest request,HttpServletResponse response){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("----------进入到法规列表控制器--------------");
        System.out.println("从前端获取的法规部号为"+lawNum);
        System.out.println("从前端获取的法规名称为"+lawName);
        model.addAttribute("lawNum",lawNum);
        model.addAttribute("lawName",lawName);
        //设置页容量
        Integer pageSize = 10;
        PageInfo<Laws> lawsList = lawsService.getLawsList(lawNum, lawName, pageNum, pageSize);
        System.out.println("aaaaaaaaaa");

        if (lawsList != null){
            System.out.println("根据条件查询有数据");
            model.addAttribute("pageInfo",lawsList);
            return "lawslist";
        }else{
            System.out.println("根据查询没有数据");
            return "redirect:/tiaozhuan";
        }
    }

    @ResponseBody
    @RequestMapping("/tiaozhuan")
    public void tiaozhuan(HttpServletResponse response,HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.write("<script>alert('查询没有数据');location.href='/findlaws'</script>");
    }



//    @ResponseBody
    @RequestMapping("/aaaa")
    public String addlaws(Laws laws){
        System.out.println(laws.toString());
//        //获取上传路径
//        String realPath = session.getServletContext().getRealPath("images/uploadfiles/");
//        //源文件名
//        String oldName = idPic.getOriginalFilename();
//        //扩展名
//        String extension = FilenameUtils.getExtension(oldName);
//        //获取文件名
//        String newName = System.currentTimeMillis()+new Random().nextInt(1000)+"_id_pic."+extension;
//        laws.setLawsPicture(newName);
//        System.out.println(laws.toString());
//        System.out.println(newName);
//        //封装File对象
//        File file = new File(realPath,newName);
        int addlaw = lawsService.addlaw(laws);
        if (addlaw > 0){
            System.out.println("添加成功");
//            out.write("<script>alert('查询没有数据');location.href='/findlaws'</script>");
        }
        return "redirect:/addlawsok";
    }
    @ResponseBody
    @RequestMapping("/addlawsok")
    public void tiaozhuan1(HttpServletResponse response,HttpServletRequest request){
        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        out.write("<script>alert('添加成功');location.href='/findlaws'</script>");
    }


    @RequestMapping("/addlaws")
    public String addlaws(){
        System.out.println("************，跳转页面");
        return "addlaws";
    }


    @GetMapping("/laws/{id}")
    public String attrStuMeg(@PathVariable("id") Integer sid, Model model){

        Laws laws = lawsService.selectByKey(sid);
        System.out.println(laws.toString());
        if (laws!=null){
            model.addAttribute("laws",laws);
            System.out.println("***********************************************************进入修改跳转");
            return "alter";
        }else {
            return null;
        }
    }

    @RequestMapping("/toupdatelaw")
    public String doupdate(Laws laws){
        System.out.println("**************************************进入修改页面");
        System.out.println(laws.toString());
        int update = lawsService.update(laws);
        if (update > 0){
            return "redirect:/findlaws";
        }
        return "null";
    }


    @GetMapping("/dellaws/{id}")
    public String dellaws(@PathVariable("id") Integer sid, Model model){

        int dellaw = lawsService.dellaw(sid);

        if (dellaw > 0){
            return "redirect:/findlaws";
        }else {
            return null;
        }
    }

    @RequestMapping("/zixun")
    public String zixun(HttpSession session, Model model, Consulting consulting,
                        HttpServletRequest request,
                        HttpServletResponse response){
        System.out.println("进入添加咨询*****************************");

        try {
            request.setCharacterEncoding("UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = null;
        try {
            out = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }

        Users attribute = (Users) session.getAttribute("users");
        if (attribute != null){
            System.out.println("用户不为空，进入添加咨询***************************");
            System.out.println(consulting.toString());
            Date date = new Date();
            consulting.setDate(date);
            int i = lawsService.addConsulting(consulting);
            out.write("<script>alert('提交成功');</script>");
            return "redirect:/ppp";
        }else {
            out.write("<script>alert('请先登录');</script>");
            return "user";
        }
    }

}
